﻿using InDepth2Demo.Chapter1.C14;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace InDepth2Demo.Chapter1.C22
{
    /// <summary>
    /// 1.2.2 查询集合
    /// </summary>
    internal class ProductQueryList
    {
        // 循环、测试和打印 C# 1
        static void Call()
        {
            ArrayList products = C11.Product.GetSampleProducts();
            foreach (C11.Product product in products)
            {
                if (product.Price > 10m)
                {
                    Console.WriteLine(product);
                }
            }
        }

        // 测试和打印分开进行 (C#2)
        static void Call2()
        {
            List<Product> products = Product.GetSampleProducts();
            Predicate<Product> test = delegate (Product p) { return p.Price > 10m; };   //匿名方法
            List<Product> matches = products.FindAll(test);
            Action<Product> print = Console.WriteLine;
            matches.ForEach(print);
        }

        // 测试和打印分开进行的另一个版本 (C#2)
        static void Call3()
        {
            List<Product> products = Product.GetSampleProducts();
            products.FindAll(delegate (Product p) { return p.Price > 10; }).ForEach(Console.WriteLine);
        }

        // 用Lambda表达式来测试 (C#3)
        static void Call4()
        {
            List<Product> products = Product.GetSampleProducts();
            foreach (Product p in products.Where(p => p.Price > 10))
            {
                Console.WriteLine(p);
            }
        }

        // 总结
        // C# 1 条件和操作紧密耦合，两者都是硬编码的
        // C# 2 条件和委托分开，匿名方法使委托变得简单
        // C# 3 Lambda 表达式使条件变得更容易阅读
    }

    class TempGenic<T1, T2>
    {

    }
}
